package com.example.system.controller;

import com.example.system.common.Result;
import com.example.system.entity.Care;
import com.example.system.entity.Wheelchair;
import com.example.system.exception.CustomException;
import com.example.system.service.impl.WheelchairServiceImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/wheelchair")
@MapperScan("com.*.mapper")
public class WheelchairController {
    @Autowired
    private WheelchairServiceImpl wheelchairService;

    //增删改查
    //新增 & 修改
    @PostMapping("/add")
    public Result addByWheelchair(@RequestBody Wheelchair wheelchair) {
        if (wheelchair.getWheelchairId() == null) {
            try {
                wheelchairService.addWheelchairByWheelchair(wheelchair);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        } else {
            try {
                wheelchairService.updateWheelchairByWheelchair(wheelchair);
            } catch (CustomException e) {
                return Result.error(e.getMsg());
            }
        }
        return Result.success();
    }

    //修改轮椅完成状态
    @PostMapping("/finish/{wheelchairId}")
    public Result updateWheelchairFinishByWheelchairId(@PathVariable Integer wheelchairId) {
        try{
            wheelchairService.updateWheelchairFinishByWheelchairId(wheelchairId);
        } catch (CustomException e){
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    // 根据轮椅Id删除轮椅信息
    @DeleteMapping("/delete/{wheelchairId}")
    public Result deleteByWheelchairId(@PathVariable Integer wheelchairId) {
        Integer deleteRes = wheelchairService.deleteWheelchairByWheelchairId(wheelchairId);
        return Result.success(deleteRes);
    }


    // 根据轮椅ID查轮椅信息
    @GetMapping("/search/self/{wheelchairId}")
    public Result findByWheelchairId(@PathVariable Integer wheelchairId) {
        Wheelchair curWheelchair = wheelchairService.findWheelchairByWheelchairId(wheelchairId);
        if (curWheelchair == null) {
            return Result.error("该轮椅不存在");
        }
        return Result.success(curWheelchair);
    }

    // 根据用户ID查轮椅信息
    @GetMapping("/search/{userId}")
    public Result findByUserId(@PathVariable Integer userId) {
        List<Wheelchair> wheelchairList = wheelchairService.findWheelchairByUserID(userId);
        if (wheelchairList == null || wheelchairList.isEmpty()) {
            return Result.error("当前用户尚无轮椅租赁记录");
        }
        return Result.success(wheelchairList);
    }

}
